package cn.jwelll.bxcstock.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import cn.jwelll.bxcstock.R;

import java.nio.charset.Charset;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 文字弹出框
 */
public class DialogUtils {
    public static final int DEFAULT = 0; //默认样式
    public static final int LOADING = 1; //进度样式

    public static class Builder {
        private Context context = null;
        private String messageString = "";
        private CharSequence charSequenceString;
        private String positive = "确定";
        private Integer positiveColor;
        private CallbackListener positiveCallbackListener = null;
        private String negative = "取消";
        private Integer negativeColor;
        private boolean isOutTouch = true;
        private CallbackListener negativeCallbackListener = null;
        private int themeResId = 0;
        private int type = DEFAULT;
        private boolean onlyPositive;
        private String title;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.messageString = message;
            return this;
        }

        public Builder setMessage(CharSequence charSequence)
        {
            charSequenceString=charSequence;
            return  this;
        }

        public Builder setOnlyPositive() {
            onlyPositive = true;
            return this;
        }

        public Builder setTitle(String msg)
        {
            title=msg;
            return  this;
        }

        public Builder setOutTouch(boolean isOutTouch) {
            this.isOutTouch = isOutTouch;
            return this;
        }

        public Builder setPositiveButton(String positive, Integer positiveColor,
                                         CallbackListener positiveCallbackListene) {
            this.positive = positive;
            this.positiveColor = positiveColor;
            positiveCallbackListener = positiveCallbackListene;
            return this;
        }
        public Builder setPositiveButton(String positive,
                                         CallbackListener positiveCallbackListene) {
            this.positive = positive;
            positiveCallbackListener = positiveCallbackListene;
            return this;
        }

        public Builder setNegativeButton(String negative, Integer negativeColor,
                                         CallbackListener negativeCallbackListene) {
            this.negative = negative;
            this.negativeColor = negativeColor;
            negativeCallbackListener = negativeCallbackListene;
            return this;
        }
        public Builder setType(int type) {
            if (type == LOADING) {
                themeResId = R.style.transparent_dialog;
                isOutTouch = false;
            }
            this.type = type;
            return this;
        }

        public Dialog creat() {
            if (context == null) {
                new Throwable("Dialog context is null");
                return null;
            }
            final Dialog dialog = new Dialog(context, themeResId);

            View contentView = null;

            switch (type) {
                case DEFAULT:
                    contentView = LayoutInflater.from(context).inflate(R.layout.dialog_prompt, null);
                    break;
                case LOADING:
                    contentView = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
                    break;
            }

            if (contentView == null) {
                return null;
            }

            //设置内容
            TextView messageTv = (TextView) contentView.findViewById(R.id.message);
            if (messageTv != null) {
                if (!messageString .isEmpty()) {
                    messageTv.setText(messageString);
                }
                if(charSequenceString!=null)
                {
                    messageTv.setText(charSequenceString);
                }
            }
            if(type!=LOADING)
            {
                TextView tvTitle=contentView.findViewById(R.id.title);
                tvTitle.setVisibility(title==null?View.GONE:View.VISIBLE);
                tvTitle.setText(title==null?"":title);
            }
            //设置确认按钮
            TextView positiveBtn =  contentView.findViewById(R.id.btn_positive);
            if (positiveBtn != null) {
                if (positive != null) {
                    positiveBtn.setText(positive);
                }
                if (positiveColor != null) {
                    positiveBtn.setTextColor(context.getResources().getColor(positiveColor));
                }
                positiveBtn.setOnClickListener(v -> {
                    dialog.dismiss();
                    if (positiveCallbackListener != null) {
                        positiveCallbackListener.onClick(v);
                    }
                });
            }

            //设置取消按钮
            TextView negativeBtn = contentView.findViewById(R.id.btn_negative);
            if (onlyPositive) {
                negativeBtn.setVisibility(View.GONE);
            }
            if (negativeBtn != null) {
                if (negative != null) {
                    negativeBtn.setText(negative);
                }
                if (negativeColor != null) {
                    negativeBtn.setTextColor(context.getResources().getColor(negativeColor));
                }
                negativeBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (negativeCallbackListener != null) {
                            negativeCallbackListener.onClick(v);
                        }
                        dialog.dismiss();
                    }
                });
            }
            //其他设置
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //设置没有标题
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setContentView(contentView);//设置内容
            dialog.setCanceledOnTouchOutside(true);//设置Dialog以外是否可触摸

            return dialog;
        }

    }

    public interface CallbackListener {
        void onClick(View v);
    }

}
